
(function() {
    var $$ = Dom7;

    var globalSequence = 0;
    $$.uniqueName = function () {
        return 'uuid_' + Date.now() + (globalSequence++);
    }
    
    $$.getJSONP = function (url, data, success) {
        if (/^([\w-]+:)?\/\/([^\/]+)/.test(url) && RegExp.$2 !== window.location.host) {
            url = url.indexOf('?') >= 0 ? (url + '&callback=') : (url + '?callback=');
        }
        url = url.indexOf('?') >= 0 ? (url + '&_nocache=' + Date.now()) : (url + '?_nocache=' + Date.now());

        if (CURRENT_USER) {
            if (typeof data === 'function') {
                success = data;
                data = {};
            }
            data = data || {};
            data.iPlanetDirectoryPro = CURRENT_USER.iPlanetDirectoryPro;
        }
        return $$.ajax({
            url:  url,
            method: 'GET',
            data: typeof data === 'function' ? undefined : data,
            success: typeof data === 'function' ? data : success,
            dataType: 'json',
            cache: false,
            crossDomain: true
        });
    };
    $$.postJSONP = function (url, data, success) {
        //if (/^([\w-]+:)?\/\/([^\/]+)/.test(url) && RegExp.$2 !== window.location.host) {
        //  url = url.indexOf('?') >= 0 ? (url + '&callback=') : (url + '?callback=');
        //}
        url = url.indexOf('?') >= 0 ? (url + '&_nocache=' + Date.now()) : (url + '?_nocache=' + Date.now());

        if (CURRENT_USER) {
            if (typeof data === 'function') {
                success = data;
                data = {};
            }
            data = data || {};
            data.iPlanetDirectoryPro = CURRENT_USER.iPlanetDirectoryPro;
        }
        return $$.ajax({
            url:  url,
            method: 'POST',
            data: typeof data === 'function' ? undefined : data,
            success: typeof data === 'function' ? data : success,
            dataType: 'json',
            cache: false,
            crossDomain: true
        });
    };

    // on some Android devices, change triggered two times and with wrong value.
    $$.delayHandler = function (fun) {
        return function (e) {
            var target = this;
            setTimeout(function () {
                fun.call(target, e)
            }, 0);
        };
    };
    
    $$(document).on('click', 'a', function (e) {
        var a = $$(this);
        var href = a.attr('href');
        if (href && href.startWith('tel:')) {
            e.preventDefault();
            if (window.TrunPage === undefined) {
                alert('TrunPage is undefined when dail to ' + href);
            } else {
                TrunPage.callSystemAbility(href);
            }
        }
    });

    // Android native
    $$('input[type="date"]').on('click', function (e) {
        //e.stopPropagation();
        var el = $$(this);
        if (!el.attr('id')) {
            el.attr('id', 'input-' + globalSequence++);
        }
        if (window.ProxyBridge !== undefined) {
            $$(this).blur();
            ProxyBridge.onInputDate(el.attr('id'), el.val());
        }
    });
    $$('input[type="time"]').on('click', function (e) {
        var el = $$(this);
        if (!el.attr('id')) {
            el.attr('id', 'input-' + globalSequence++);
        }
        if (window.ProxyBridge !== undefined) {
            $$(this).blur();
            ProxyBridge.onInputTime(el.attr('id'), el.val());
        }
    });
    
    function onDateSet(id, text) {
        $$('#' + id).val(text);
    }
    function onTimeSet(id, text) {
        $$('#' + id).val(text);
    }
    
    window.android1 = {
        onDateSet: onDateSet,
        onTimeSet: onTimeSet
    };
}());
